#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include <QSerialPort>
#include <QTableWidgetItem>
#include "login/logindialog.h"
#include "check_output.h"
#include "checkvaluecontainer.h"
#include "classvaluecontainer.h"
#include <QList>
#include "ImportExport/exporttool.h"

namespace Ui {
class MainWidget;
}

class MainWidget : public QWidget
{
    Q_OBJECT

public:

    ~MainWidget();
    static MainWidget *getObj();
    //inline static QSerialPort* getSp() {return mSp;}
    void changeUser(const QString& str);

    enum UseMode{
        ELIMINATE = 0,//剔除
        CLASSIFIER//分选
    };
    enum CheckMode{
        STABELMODE = 0,//稳定
        DYNAMIC//动态
    };
    enum ChkResult{
        CHECK_NONE = 0,
        CHECK_LOW,
        CHECK_OK,
        CHECK_HIGH,
        CHECK_END
    };
    enum ClassResult{
        CLASS_NONE = 0,CLASS_R1,CLASS_R2,CLASS_R3,CLASS_R4,CLASS_R5,CLASS_R6,CLASS_R7,
        CLASS_R8,CLASS_R9,CLASS_R10,CLASS_R11,CLASS_R12,CLASS_END
    };
    void initUseMode();
    void initCheckMode();
    void setHi(float hi);
    void setLow(float low);
public slots:
    void slot_testin(bool);
    void slot_testout(bool);
    void slot_test1(bool);
    void slot_test2(bool);
    void updateScalePara();
    void tareScale(float w=0);
    void preTareScale(float weight);
    void zeroScale();
    void syncPa();

private slots:
    void onUpdateScale();
    void onUpdateTare(const QString&);
    void onHasLogin();
    void on_btn_com_clicked();
    //void onReadyRead();

    //void on_btn_com_5_clicked();

    void on_btn_menu_clicked();

    void on_btn_user_clicked();
    void login(const QModelIndex&);
    void changeProduct(const QModelIndex&);
    void changeClassProduct(const QModelIndex&);

    void on_btn_lock_clicked();

    void on_btn_start_clicked();

    void on_btn_stop_clicked();

    void on_btn_clear_clicked();

    void on_btn_zero_clicked();

    void on_btn_tare_clicked();

    void on_btn_preTare_clicked();

    void onAcceptPreTare(QString);

    void onAcceptFontSpeed(QString);
    void onAcceptWeighingSpeed(QString);
    void onAcceptBackSpeed(QString);

    void toInputCheckValue(int);
    void onAcceptLowVlue(QString);
    void onAcceptOkVlue(QString);
    void onAcceptHiVlue(QString);

private:
    Ui::MainWidget *ui;
    explicit MainWidget(QWidget *parent = 0);
    static MainWidget* mMainWidget;
    bool eventFilter(QObject *, QEvent *);

    void checkWeight(float net);
    void checkLOH(float net);
    void changeChkColor();
    void setCheckOutput();
    void checkClass(float net);
    void saveRecord();
    void recordSaveToDB();
    void saveToRecordHLO();
    void saveToRecordClass();
    void updateCheckWidget();
    void updateClassWidget();
    void updateStabFlag();
    void updateZeroFlag();
    void updateCheckStatus(bool);

    void clearEliminate();
    void clearClass();
    //void initCom();
    //static QSerialPort *mSp;
    void initMainValueWidget();

    bool mSwitch;
    bool mCanUpdateScale;
    int mStabCnt;
    LoginDialog *lg;
    QString mUser;
    QString mProduct;
    QString mBrand;
    QString mBarcode;
    float mHi;
    float mOk;
    float mLo;
    //QList<Output::Range> mRlist;
    QMap<int,Output::Range> mRmap;
    float mLastStabNet;
    bool getStabNet;
    bool mHaveChecked;
    int mRecordNo;
    QMap<QString,CheckValueContainer*> mChkValuesMap;
    QMap<QString,ClassValueContainer*> mClassValuesMap;
    UseMode mUseMode;
    CheckMode mCheckMode;
    bool mAutoUsbCopy;
    ChkResult mChkResult;
    ClassResult mClassResult;
    ExportTool* mExportTool;

};

#endif // MAINWIDGET_H
